package com.zx.pm.modules.zx.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.impl.util.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zx.pm.common.config.Global;
import com.zx.pm.common.mapper.JsonMapper;
import com.zx.pm.common.persistence.Page;
import com.zx.pm.common.utils.StringUtils;
import com.zx.pm.common.web.BaseController;
import com.zx.pm.modules.zx.entity.ExpertsInfo;
import com.zx.pm.modules.zx.entity.Lzd;
import com.zx.pm.modules.zx.entity.ProExperts;
import com.zx.pm.modules.zx.service.ExpertsInfoService;
import com.zx.pm.modules.zx.service.LzdService;
import com.zx.pm.modules.zx.service.ProExpertsService;
import com.zx.pm.modules.zx.service.TenderSectionService;

/**
 * 项目评审专家管理Controller
 * @author forest
 * @version 2019-01-18
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/proExperts")
public class ProExpertsController extends BaseController{

	@Autowired
	private LzdService lzdService;
	
	@Autowired
	private ProExpertsService proExpertsService;

	@Autowired
	private ExpertsInfoService expertsInfoService;

	@Autowired
	private TenderSectionService tenderSectionService;
	
	@ModelAttribute
	public Lzd get(@RequestParam(required=false) String id) {
		Lzd entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = lzdService.get(id);
		}
		if (entity == null){
			entity = new Lzd();
		}
		return entity;
	}
	
	@RequiresPermissions("zx:proExperts:view")
	@RequestMapping(value = {"list", ""})
	public String list(Lzd lzd, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Lzd> page = lzdService.findSectionPage(new Page<Lzd>(request, response), lzd); 
		model.addAttribute("page", page);
		return "modules/zx/proExpertsList";
	}

	@RequiresPermissions("zx:proExperts:view")
	@RequestMapping(value = "form")
	public String form(Lzd lzd, Model model) {
		ProExperts p = new ProExperts();
		p.setLzdId(lzd.getId());
		p.setSectionId(lzd.getSectionId());
		List<ProExperts> peList = proExpertsService.findListByLzdId(p);
		// 获取标段信息
		lzd.setTs(tenderSectionService.get(lzd.getSectionId()));
		lzd.setDepositMoney(lzdService.getDepositMoney(lzd.getId(), lzd.getSectionId()));
		model.addAttribute("lzd", lzd);
		model.addAttribute("peList", peList);
		return "modules/zx/proExpertsForm";
	}
	
	@RequiresPermissions("zx:proExperts:show")
	@RequestMapping(value = "show")
	public String show(Lzd lzd, Model model) {
		ProExperts p = new ProExperts();
		p.setLzdId(lzd.getId());
		p.setSectionId(lzd.getSectionId());
		List<ProExperts> peList = proExpertsService.findListByLzdId(p);
		// 获取标段信息
		lzd.setTs(tenderSectionService.get(lzd.getSectionId()));
		lzd.setDepositMoney(lzdService.getDepositMoney(lzd.getId(), lzd.getSectionId()));
		model.addAttribute("lzd", lzd);
		model.addAttribute("peList", peList);
		return "modules/zx/proExpertsShow";
	}
	
	@RequiresPermissions("zx:proExperts:edit")
	@RequestMapping(value = "delete")
	public String delete(ProExperts proExperts, RedirectAttributes redirectAttributes) {
		proExpertsService.delete(proExperts);
		addMessage(redirectAttributes, "移除项目评审专家成功");
		return "redirect:"+Global.getAdminPath()+"/zx/proExperts/form/?id="+ proExperts.getLzdId() +"&sectionId="+proExperts.getSectionId()+"&repage";
	}
	
	
	@RequiresPermissions("zx:proExperts:edit")
	@RequestMapping(value = "addProExerts")
	public String addProExerts(HttpServletRequest request, Model model) { 
		model.addAttribute("lzdId", request.getParameter("lzdId"));
		model.addAttribute("sectionId", request.getParameter("sectionId"));
		return "modules/zx/addProExerts";
	}
	
	// 保存项目供应商信息
	@RequestMapping(value = "saveExertsInfo")
	@ResponseBody
	public String saveExertsInfo(ExpertsInfo expertsInfo){
		// 判断专家信息是否存在，存在，则不允许添加
		/*if(expertsInfoService.expertsExist(expertsInfo) > 0) {
			return "1";
		}*/
		
		ProExperts pe = new ProExperts();
		pe.setExpertsId(expertsInfo.getId()); //专家信息
		pe.setLzdId(expertsInfo.getLzdId());
		pe.setSectionId(expertsInfo.getSectionId());
		pe.setPayFlag("0"); // 默认为未支付状态
		// 保存专家信息
		if(StringUtils.isBlank(expertsInfo.getId())) {
			expertsInfo.preInsert();
			pe.setExpertsId(expertsInfo.getId()); //专家信息
			expertsInfoService.saveExpertsInfo(expertsInfo);
		}else {
			// 判断该专家是否存在与该项目，如果存在，则不允许添加
			if(proExpertsService.getRelationExits(pe) > 0) {
				return "1";
			}
		}
		// 保存专家与项目关联关系
		proExpertsService.save(pe);
		//addMessage(redirectAttributes, "供应商信息保存成功");
		return "0";
	}
	
	
	@RequestMapping(value="getExpertsInfo")
	@ResponseBody
	public ExpertsInfo getExpertsInfo(HttpServletRequest request) {
		String card = request.getParameter("card");
		ExpertsInfo e = expertsInfoService.getExpertsInfo(card);
		return  e;
	}
	
	@RequiresPermissions("zx:proExperts:edit")
	@RequestMapping(value = "myExertsList")
	public String myExertsList(HttpServletRequest request, Model model, ExpertsInfo expertsInfo) { 
		// 获取供应商列表
		List<ExpertsInfo> list = expertsInfoService.findList(expertsInfo);
		// 获取项目供应商列表
		ProExperts pe = new ProExperts();
		pe.setLzdId(request.getParameter("lzdId"));
		pe.setSectionId(request.getParameter("sectionId"));
		List<ProExperts> peList = proExpertsService.findListByLzdId(pe);
		// 判断供应商是否已经分配给该项目了
		for (ExpertsInfo e : list) {
			for (ProExperts p : peList) {
				if (p.getExpertsId().equals(e.getId())) {
					e.setFenPeiFlag("1");
					break;
				}
			}
		}
		model.addAttribute("sectionId", request.getParameter("sectionId"));
		model.addAttribute("lzdId", request.getParameter("lzdId"));
		model.addAttribute("peList", list);
		model.addAttribute("expertsInfo", expertsInfo);
		return "modules/zx/myExpertsList";
	}
	
	@RequiresPermissions("zx:proExperts:edit")
	@RequestMapping(value = "savePsRole")
	public String savePsRole(HttpServletRequest request, RedirectAttributes redirectAttributes) {
		String lzdId = request.getParameter("lzdId");
		String sIds = request.getParameter("sIds");
		String sectionId = request.getParameter("sectionId");
		String[] arrayId = sIds.split(",");
		ProExperts pe = null;
		// 保存评审专家信息
		for (String str : arrayId) {
			pe = new ProExperts();
			pe.setExpertsId(str);
			pe.setLzdId(lzdId);
			pe.setPayFlag("0"); // 默认为未支付状态
			pe.setSectionId(sectionId);
			proExpertsService.save(pe);
		}
		addMessage(redirectAttributes, "信息保存成功");
		return "redirect:"+Global.getAdminPath()+"/zx/proExperts/form/?id="+ lzdId+"&sectionId=" + sectionId + "&repage";
	}
	
	@RequestMapping(value = "pay")
	public String pay(HttpServletRequest request, RedirectAttributes redirectAttributes, ProExperts proExperts) {
		proExperts.setId(request.getParameter("proExpertsId"));
		proExperts.setPayTime(new Date());
		proExperts.setPayFlag("1");
		proExpertsService.updatePay(proExperts);
		addMessage(redirectAttributes, "专家付费信息保存成功");
		return "redirect:"+Global.getAdminPath()+"/zx/proExperts/form/?id="+ proExperts.getLzdId() +"&sectionId="+proExperts.getSectionId()+"&repage";
	}
	
	@RequestMapping(value = "batchPay")
	public String batchPay(HttpServletRequest request, RedirectAttributes redirectAttributes, Lzd lzd) {
		if(lzd != null && lzd.getPayList().size() > 0) {
			for(ProExperts proExperts : lzd.getPayList()) {
				if(proExperts.getPaySum() != null && proExperts.getPaySum() > 0) {
					proExperts.setPayTime(new Date());
					proExperts.setPayFlag("1");
					proExpertsService.updatePay(proExperts);
				}
			}
		}
		addMessage(redirectAttributes, "专家付费信息保存成功");
		return "redirect:"+Global.getAdminPath()+"/zx/proExperts/form/?id="+ lzd.getId() +"&sectionId="+lzd.getSectionId()+"&repage";
	}
	
	
	@RequestMapping(value = "batchConfirmPay")
	public String batchConfirmPay(HttpServletRequest request, RedirectAttributes redirectAttributes, Lzd lzd) {
		if(lzd != null && lzd.getPayList().size() > 0) {
			for(ProExperts proExperts : lzd.getPayList()) {
				if(StringUtils.isNotBlank(proExperts.getPayFlag())) {
					proExpertsService.batchConfirmPay(proExperts);
				}
			}
		}
		addMessage(redirectAttributes, "信息保存成功");
		return "redirect:"+Global.getAdminPath()+"/zx/proExperts/show/?id="+ lzd.getId() +"&sectionId="+lzd.getSectionId()+"&repage";
	}
}


